package com.zdkj.subway.support.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.data.mybatis.wrapper.CustomQueryWrapper;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.dto.in.SwParamReceiptDTO;
import com.zdkj.subway.support.entity.SwParamReceipt;
import lombok.extern.slf4j.Slf4j;
import com.zdkj.subway.support.service.SwParamReceiptService;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 发票信息表
 *
 * @author cui_com
 * @date 2020-01-08 17:13:42
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swparamreceipt")
@Api(value = "swparamreceipt", tags = "发票信息表管理")
public class SwParamReceiptController {

    private final SwParamReceiptService swParamReceiptService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param swParamReceiptDTO 发票信息表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/detail/page")
    public R getSwParamReceiptDetailPage(Page page, SwParamReceiptDTO swParamReceiptDTO) {
        return R.ok(swParamReceiptService.queryStationDetailByPage(
                page, new CustomQueryWrapper(SwParamReceipt.class)
                        .addDTO(swParamReceiptDTO)
                        .init()));
    }

    /**
     * 根据类型查面额
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/dict/byType/{type}")
    public R getSwParamReceiptDetailPage(@PathVariable("type") String type) {
        SwParamReceiptDTO swParamReceiptDTO = new SwParamReceiptDTO();
        swParamReceiptDTO.setRefDictReceiptType(type);
        return R.ok(swParamReceiptService.list(
                new CustomQueryWrapper(SwParamReceipt.class)
                        .addDTO(swParamReceiptDTO)
                        .init()));
    }

    /**
     * 查面额
     *
     * @return
     */
    @ApiOperation(value = "查面额", notes = "查面额")
    @GetMapping("/dict/byType")
    public R getSwParamReceiptDetailPage() {
        return R.ok(swParamReceiptService.list());
    }

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param swParamReceipt 发票信息表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwParamReceiptPage(Page page, SwParamReceipt swParamReceipt) {
        return R.ok(swParamReceiptService.page(page, Wrappers.query(swParamReceipt)));
    }


    /**
     * 通过id查询发票信息表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swParamReceiptService.getById(id));
    }

    /**
     * 新增发票信息表
     *
     * @param swParamReceipt 发票信息表
     * @return R
     */
    @ApiOperation(value = "新增发票信息表", notes = "新增发票信息表")
    @SysLog("新增发票信息表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_receipt_add')")
    public R save(@RequestBody SwParamReceiptDTO swParamReceipt) {
        SwParamReceipt defaultObj = ObjectUtils.getDefaultObj(SwParamReceipt.class);
        BeanUtils.copyProperties(swParamReceipt, defaultObj);
        return R.ok(swParamReceiptService.save(defaultObj));
    }

    /**
     * 修改发票信息表
     *
     * @param swParamReceipt 发票信息表
     * @return R
     */
    @ApiOperation(value = "修改发票信息表", notes = "修改发票信息表")
    @SysLog("修改发票信息表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_receipt_edit')")
    public R updateById(@RequestBody SwParamReceiptDTO swParamReceipt) {
        SwParamReceipt obj = new SwParamReceipt();
        BeanUtils.copyProperties(swParamReceipt, obj);
        return R.ok(swParamReceiptService.updateById(obj));
    }

    /**
     * 通过id删除发票信息表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除发票信息表", notes = "通过id删除发票信息表")
    @SysLog("通过id删除发票信息表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_param_receipt_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swParamReceiptService.removeById(id));
    }
    /**
     * 导入发票信息
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入发票信息", notes = "导入发票信息")
    @SysLog("导入发票信息")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwParamReceipt.class,
                    new UploadDataListener(swParamReceiptService, SwParamReceipt.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }

}
